package com.yidu.hbatsams.utils.ExportWordTools;

import com.yidu.hbatsams.domain.AtsScheme;
import com.yidu.hbatsams.domain.AtsSchemeAudit;
import com.yidu.hbatsams.domain.AtsSchemeExpert;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @描述：加入Word表格数据
 * @ClassName: hbats
 * @backage: com.yidu.hbats.action.AtsScheme
 * @Date: 2019/12/1 2:56
 * @Author:胡立
 **/
public class ExportWordAddData {
    /**
     * 加入数据
     */
    public XWPFDocument exportWord(String userName, AtsScheme atsScheme, String SchemeAthlete, AtsSchemeAudit atsSchemeAudit, AtsSchemeExpert atsSchemeExpert, String contextPath){
        ExportWord ew = new ExportWord();
        XWPFHelper xh=new XWPFHelper();
        XWPFParagraph xp=null;

        TextStyle ts=new TextStyle();
        XWPFDocument document = ew.createXWPFDocument();

        List<List<Object>> list = new ArrayList<List<Object>>();

        //以下为加入数据
        List<Object> tempList = new ArrayList<Object>();
        tempList.add("方案名称");
        tempList.add(atsScheme.getSchemeName());
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("教练员");
        tempList.add(changeStr(atsScheme.getCoachName()));
        tempList.add("方案类型");
        if("1".equals(atsScheme.getSchemeType())){
            tempList.add("运动康复方案");
        }else if("2".equals(atsScheme.getSchemeType())){
            tempList.add("运动营养方案");
        }else if("3".equals(atsScheme.getSchemeType())){
            tempList.add("心理训练方案");
        }
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("方案年份");
        tempList.add(atsScheme.getYear());
        tempList.add("方案月份");
        tempList.add(atsScheme.getMonth());
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("创建时间");
        tempList.add(changeStr(new SimpleDateFormat("yyyy-MM-dd").format(atsScheme.getCreatTime())));
        tempList.add("创建人");
        tempList.add(changeStr(atsScheme.getCreater()));
        list.add(tempList);

        tempList = new ArrayList<Object>();
        tempList.add("运动员");
        tempList.add(changeStr(SchemeAthlete));
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("审核状态");
        if("10".equals(atsSchemeAudit.getAuditState())){
            tempList.add("已提交申请");
        }else if("20".equals(atsSchemeAudit.getAuditState())){
            tempList.add("已同意");
        }else if("21".equals(atsSchemeAudit.getAuditState())){
            tempList.add("未同意");
        }else if("30".equals(atsSchemeAudit.getAuditState())){
            tempList.add("已提交待回复");
        }else if("40".equals(atsSchemeAudit.getAuditState())){
            tempList.add("专家已回复");
        }
        tempList.add("申请人");
        tempList.add(changeStr(atsSchemeExpert.getCreater()));
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("申请理由");
        tempList.add(changeStr(atsSchemeExpert.getApplyForExp()));
        list.add(tempList);

        tempList = new ArrayList<Object>();
        tempList.add("审核人");
        tempList.add(changeStr(atsSchemeAudit.getCreater()));
        tempList.add("审核时间");
        tempList.add(changeStr(atsSchemeAudit.getAuTime()));
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("审核意见");
        tempList.add(changeStr(atsSchemeAudit.getAuExp()));
        list.add(tempList);

        tempList = new ArrayList<Object>();
        tempList.add("指定专家");
        tempList.add(changeStr(atsSchemeExpert.getExpertName()));
        tempList.add("专家回复时间");
        tempList.add(changeStr(atsSchemeExpert.getRemark()));

        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("专家咨询结果");
        tempList.add(changeStr(atsSchemeExpert.getResults()));
        list.add(tempList);

        tempList = new ArrayList<Object>();
        tempList.add("附件一");
        tempList.add(changeStr(atsScheme.getFile1()));
        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("附件二");
        tempList.add(changeStr(atsScheme.getFile2()));

        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("附件三");
        tempList.add(changeStr(atsScheme.getFile3()));

        list.add(tempList);
        tempList = new ArrayList<Object>();
        tempList.add("反馈附件");
        tempList.add(changeStr(atsScheme.getResultFile()));
        list.add(tempList);

        tempList = new ArrayList<Object>();
        tempList.add("导出人");
        tempList.add(changeStr(userName));
        tempList.add("导出时间");
        tempList.add(changeStr(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
        list.add(tempList);


        Map<String, Object> dataList = new HashMap<String, Object>();
        dataList.put("TITLE", atsScheme.getSchemeName());
        dataList.put("TABLEDATA", list);
        try {
            ew.exportCheckWord(dataList, document,contextPath);
            return document;
        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }

    }

    /**
     * 如果为空则返回""
     * @param str
     * @return
     */
    public String changeStr(String str){
        if(str!=null&&!"".equals(str)){
            return str;
        }else{
            return "";
        }
    }
}
